#include "myslide.h"
#include "ui_myslide.h"
#include"qmath.h"

class TuneManualFrame;

MySlide::MySlide(QWidget *parent,QString title,QString actionLeft,
                 QString unit,QString actionRight) :
    QFrame(parent),
    ui(new Ui::MySlide)
{
    ui->setupUi(this);//QwtSlider
    valueMin=0;
    valueMax=0;
    valueCurrent=0;//
    valueSet=0;
    num=0;
    bIsValueSet=false;

    ui->label_Title->setText(title);
    ui->label_ActionLeft->setText(actionLeft);
    //ui->label_ValueLeft->setText(valueLeft);
    ui->label_Unit->setText(unit);
    ui->label_ActionRight->setText(actionRight);
    //ui->label_ValueRight->setText(valueRight);

    connect(ui->horizontalSlider,SIGNAL(valueChanged(double)),this,SLOT(UpdateValue(double)));
    connect(ui->pushButton_Plus,SIGNAL(pressed()),this,SLOT(PlusValue()));
    connect(ui->pushButton_Minus,SIGNAL(pressed()),this,SLOT(MinusValue()));
    //TuneManualFrame *pTuneManualFrame=qobject_cast<TuneManualFrame *>(parent);
    connect(this,SIGNAL(SIG_ValueSet()),parent,SLOT(OnTuneParaSet()));

    ui->pushButton_Minus->installEventFilter(this);
    ui->pushButton_Plus->installEventFilter(this);
    ui->horizontalSlider->installEventFilter(this);
}

MySlide::~MySlide()
{
    delete ui;
}

bool MySlide::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type()==QEvent::MouseButtonRelease)
    {
        if(bIsValueSet==true)
        {
            bIsValueSet=false;
            emit SIG_ValueSet();
        }
    }
    else if(event->type()==QEvent::MouseButtonPress)
    {
        bIsValueSet=false;
    }

    return QObject::eventFilter(obj, event);
}

double MySlide::GetValueSet()
{
   return valueSet;
}

void MySlide::SetValueSet(double value)
{
    valueSet=value;
    ui->horizontalSlider->setValue(value);
    ui->label_ValueRight->setText(QString::number(value,'f',num));

    bIsValueSet=true;
}
void MySlide::SetValuePre(double value)
{
    valuePre=value;
    ui->label_ValueLeft->setText(QString::number(value,'f',num));
}
void MySlide::SetValueCurrent(double value)
{
    valueCurrent=value;
    ui->label_ValueSet->setText(QString::number(value,'f',num));
}

void MySlide::SetScope(double min,double max,int nummer)
{
    num=nummer;
    valueMin=min;
    valueMax=max;
    totalSteps=(max-min)*qPow(10,num);
    ui->horizontalSlider->setScale(min,max);
    ui->horizontalSlider->setValue(min);
    ui->horizontalSlider->setTotalSteps(totalSteps);

    SetValueSet(valueMin);
    SetValuePre(valueMin);
    SetValueCurrent(valueMin);
}

void MySlide::SetValue(double value) //
{
    SetValueSet(value);
    SetValuePre(value);
}

//slot
void MySlide::UpdateValue(double value)//滚动设置事件
{
    SetValueSet(value);
}
void MySlide::PlusValue()//+
{
    if(valueSet>=valueMax) return;
    double value=valueSet+(valueMax-valueMin)/totalSteps;
    SetValueSet(value);
}
void MySlide::MinusValue()//-
{
    if(valueSet<=valueMin) return;
    double value=valueSet-(valueMax-valueMin)/totalSteps;
    SetValueSet(value);
}
